<?php
namespace App\Admin\Models;
use Common\BaseModel;
class UserMonthIncome extends BaseModel
{
    protected $site;
    protected $redis_key = "";

    public function initialize()
    {
        parent::initialize();
        $this->setSource('user_month_income');
    }

    public function add_log($data, $date)
    {
        //	$wallet= new Wallets();
        $result = [];
        foreach ($data as $k => $v) {
            $condition['conditions'] = "month ='$date' and user_id=$k";
            $money                   = $this->findFirst($condition);
            $is_add                  = 0;
            //之前有了
            if ($money == false) {
                $money = new UserMonthIncome();
            }
            $money->user_id    = $k;
            $money->commission = $v['commission'];
            $money->market     = $v['market'];
            $money->month      = $date;
            if (!$money->save()) {
                return false;
            }
            $result[] = $money->toarray();
        }
        //die('ww');
        return $result;
    }

    public function get_income($user_id)
    {
        $condition['conditions'] = "user_id=$user_id";
        $condition['columns']    = 'commission,market,month';
        return $this->find($condition)->toarray();

    }

    public function is_up_founder($user_id)
    {
        $config                  = 1000;
        $time                    = date('Y-m-d', strtotime('-180 day'));
        $condition['conditions'] = "user_id=$user_id and (commission+market) >= $config and month >= '$time'";
        $condition['columns']    = 'id';
        $res                     = $this->findFirst($condition);
        if ($res == false) {
            return false;
        }
        return true;
    }

    public function get_all_income($user_id)
    {
        $condition['conditions'] = "user_id=$user_id";
        $condition['columns']    = 'sum(commission+market+area+ad+red+new_people+new_area_people) as money';
        $res                     = $this->findFirst($condition);
        if (!isset($res->money) || empty($res->money)) {
            return "0.00";
        }
        return $res->money . "";
    }

    //核账接口
    public function get_all_money()
    {
        $condition['columns'] = 'sum(commission+market) as money,user_id';
        $condition['group']   = 'user_id';
        //$condition['limit']=1;
        $res = $this->find($condition);
        return $res->toarray();
    }

    public function get_count_list($data)
    {
        $conditions = '';

        $page = isset($data['page']) ? $data['page'] : 1;
        $size = 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }

        $fitst['group']   = 'user_id';
        $fitst['columns'] = 'count(id) as count';
        $count            = $this::findFirst($fitst);
        $result['count']  = $count->count ? $count->count : 0;

        $result['list']  = [];
        if ($result['count'] > 0) {
            $cate_list      = $this::find(
                array(
                    $conditions,
                    "columns" => 'sum(commission+market+area) as money,user_id',
                    'limit'   => $size,
                    'offset'  => $page,
                    'group'   => 'user_id',
                    'order'   => 'money ' . $data['sort'],
                ));
            $result['list'] = $cate_list->toarray();
        }
        return $result;
    }



    public function add_other_log($res, $date, $column)
    {
        $result = [];
        foreach ($res as $k => $v) {
            $condition['conditions'] = "user_id= {$v['user_id']} and month='{$date}'";
            $info                    = $this->findFirst($condition);
            if ($info == false) {
                $info             = new UserMonthIncome();
                $info->commission = 0;
                $info->market     = 0;
                $info->month      = $date;
                $info->user_id    = $v['user_id'];
            }
            $info->$column = $v['money'];
            if (!$info->save()) {
                return false;
            }
            $result[] = $info->toarray();
        }
        return $result;

    }

    public function is_income($params){
        $month = date('Y-m',strtotime($params['start_at']));
        $where['conditions'] = "month = '$month' and user_id in({$params['user_id']})";
        $where['columns'] = "user_id";
        $list = $this->find($where)->toarray();
        if(!empty($list)){
            return implode(',',array_column($list,'user_id'));
        }
        return '';
    }
}
